package com.permission.controller;

import com.permission.dto.request.role.*;
import com.permission.message.StandardResponse;
import com.permission.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @desc: 角色基本接口
 * @author: xhs
 * @date: 2021/10/14 16:50
 * @version: JDK 1.8
 */
@Slf4j
@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private SysRoleService roleService;

    /**
     * 分页查询角色接口
     *
     * @param rolePageRequest
     * @return
     */
    @PostMapping("/queryRole")
    public StandardResponse queryRole(@Valid @RequestBody RolePageRequest rolePageRequest) {
        log.info("-----------------【分页查询角色接口入参 rolePageRequest：{}】--------------", rolePageRequest.toString());
        StandardResponse response = roleService.selectRole(rolePageRequest);
        return response;
    }

    /**
     * 新增角色接口
     *
     * @param request
     * @param roleRequest
     * @return
     */
    @PostMapping("/insertRole")
    public StandardResponse insertRole(HttpServletRequest request, @Valid @RequestBody InsertRoleRequest roleRequest) {
        log.info("-----------------【新增角色接口入参 roleRequest：{}】--------------", roleRequest.toString());
        StandardResponse response = roleService.insertRole(request, roleRequest);
        return response;
    }

    /**
     * 删除角色接口
     *
     * @param request
     * @param delRoleRequest
     * @return
     */
    @PostMapping("/delRole")
    public StandardResponse delRole(HttpServletRequest request, @Valid @RequestBody DelRoleRequest delRoleRequest) {
        log.info("-----------------【删除角色接口入参 delRoleRequest：{}】--------------", delRoleRequest.toString());
        StandardResponse response = roleService.delRole(request, delRoleRequest);
        return response;
    }

    /**
     * 编辑角色接口
     *
     * @param request
     * @param updateRoleRequest
     * @return
     */
    @PostMapping("/updateRole")
    public StandardResponse updateRole(HttpServletRequest request, @Valid @RequestBody UpdateRoleRequest updateRoleRequest) {
        log.info("-----------------【编辑角色接口入参 updateRoleRequest：{}】--------------", updateRoleRequest.toString());
        StandardResponse response = roleService.updateRole(request, updateRoleRequest);
        return response;
    }

    /**
     * 根据id查询角色接口
     *
     * @param request
     * @param delRoleRequest
     * @return
     */
    @PostMapping("/getRoleById")
    public StandardResponse getRoleById(HttpServletRequest request, @Valid @RequestBody DelRoleRequest delRoleRequest) {
        log.info("-----------------【根据id查询角色接口入参 delRoleRequest：{}】--------------", delRoleRequest.toString());
        StandardResponse response = roleService.getRoleById(request, delRoleRequest);
        return response;
    }

    /**
     * 根据角色Id查询权限接口
     *
     * @param permissionsRequest
     * @return
     */
    @PostMapping("/getPermissions")
    public StandardResponse getPermissions(@Valid @RequestBody SelectPermissionsRequest permissionsRequest) {
        log.info("-----------------【根据id查询权限接口入参 permissionsRequest：{}】--------------", permissionsRequest.toString());
        StandardResponse response = roleService.getPermissions(permissionsRequest);
        return response;
    }

    /**
     * 分配权限
     *
     * @param permissionsRequest
     * @return
     */
    @PostMapping("/addPermissions")
    public StandardResponse addPermissions(@Valid @RequestBody AssignPermissionRequest permissionsRequest) {
        log.info("-----------------【分配权限接口入参 permissionsRequest：{}】--------------", permissionsRequest.toString());
        StandardResponse response = roleService.addPermissions(permissionsRequest);
        return response;
    }

    /**
     * 查询角色列表
     *
     * @return
     */
    @PostMapping("/getRole")
    public StandardResponse getRole() {
        StandardResponse response = roleService.getRole();
        return response;
    }

    /**
     * 根据用户ID查询角色
     *
     * @return
     */
    @PostMapping("/getRoleByUserId")
    public StandardResponse getRoleByUserId(@Valid @RequestBody QueryRoleRequest queryRoleRequest) {
        log.info("-----------------【根据用户ID查询角色接口入参 permissionsRequest：{}】--------------", queryRoleRequest.toString());
        StandardResponse response = roleService.getRoleByUserId(queryRoleRequest);
        return response;
    }


}
